package FIlter;

import javax.management.remote.rmi.RMIConnection;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "tokenFilter")
public class tokenFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req=(HttpServletRequest) request;
        HttpServletResponse resp=(HttpServletResponse) response;
        String token = request.getParameter("token");
        HttpSession session = req.getSession(true);
        if(token==null){
            System.out.println("这个通过一次");
            resp.sendRedirect("https://cas.dgut.edu.cn?appid=javaee&state=STATE");
        }
        else{
            System.out.println(token);
            System.out.println("通过一次");
            session.setAttribute("token", token);
            chain.doFilter(req,resp);
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
